package cn.tpshion.task.config;

import cn.tpshion.task.config.support.GoTaskProperties;
import cn.tpshion.task.server.ClientHandler;
import cn.tpshion.task.server.HeartbeatHandler;
import cn.tpshion.task.server.NettyClient;
import cn.tpshion.task.server.TaskChannelInitializer;
import cn.tpshion.task.task.MsgConnHandler;
import cn.tpshion.task.task.MsgHandlerFactory;
import cn.tpshion.task.task.MsgHeartbeatHandler;
import cn.tpshion.task.task.MsgTaskHandler;
import cn.tpshion.task.task.TaskManager;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Configuration
@EnableConfigurationProperties({GoTaskProperties.class})
@ConditionalOnProperty(prefix = "go-task", name = "enabled", havingValue = "true", matchIfMissing = true)
public class GoTaskAutoConfig {

    @Bean
    public TaskManager taskManager(ThreadPoolTaskScheduler goTaskExecutor) {
        return new TaskManager(goTaskExecutor);
    }

    @Bean
    public ClientHandler clientHandler(GoTaskProperties properties, MsgHandlerFactory factory, ThreadPoolTaskExecutor goMsgExecutor) {
        return new ClientHandler(properties, factory, goMsgExecutor);
    }

    @Bean
    public HeartbeatHandler heartbeatHandler() {
        return new HeartbeatHandler();
    }

    @Bean
    public TaskChannelInitializer taskChannelInitializer(HeartbeatHandler heartbeatHandler, ClientHandler clientHandler) {
        return new TaskChannelInitializer(heartbeatHandler, clientHandler);
    }

    @Bean
    public NettyClient nettyClient(GoTaskProperties properties, TaskChannelInitializer taskChannelInitializer) {
        return new NettyClient(properties, taskChannelInitializer);
    }

    @Bean("goMsgExecutor")
    public ThreadPoolTaskExecutor goMsgExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("go-msg-");
        // 设置线程活跃时间（秒）
        executor.setKeepAliveSeconds(60);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        // 初始化线程池
        executor.initialize();
        return executor;
    }

    @Bean("goTaskExecutor")
    public ThreadPoolTaskScheduler goTaskExecutor() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        // 设置线程池大小
        taskScheduler.setPoolSize(10);
        // 设置线程名称前缀
        taskScheduler.setThreadNamePrefix("go-task-");
        taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        taskScheduler.setAwaitTerminationSeconds(60);
        return taskScheduler;
    }

    @Bean
    public MsgHandlerFactory msgHandlerFactory() {
        return new MsgHandlerFactory();
    }

    @Bean
    public MsgConnHandler msgConnHandler() {
        return new MsgConnHandler();
    }

    @Bean
    public MsgHeartbeatHandler msgHeartbeatHandler() {
        return new MsgHeartbeatHandler();
    }

    @Bean
    public MsgTaskHandler msgTaskHandler(TaskManager taskManager) {
        return new MsgTaskHandler(taskManager);
    }

}
